package ui.swing.tree;

import java.awt.Font;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;

public class HashtableTreeDemo extends JFrame {

	public HashtableTreeDemo() {
		super("我是用Hashtable建的树");
		setBounds(200, 200, 320, 500);

		Font font = new Font("Dialog", Font.PLAIN, 12);
		Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
		/** 定义Windows风格界面 **/
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			if (UIManager.get(key) instanceof Font) {
				UIManager.put(key, font);
			}
		}
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception el) {
			System.exit(0);
		}
		
		

		String[] s1 = { "公司文件", "个人信件", "私人文件" };
		String[] s2 = { "本机磁盘（c:）", "本机磁盘（d:）", "本机磁盘（e:）" };
		String[] s3 = { "百度", "Google", "必应" };
		Vector v4 = new Vector();
		v4.add("新浪");
		v4.add("凤凰网");
		v4.add("网易");
		v4.add("搜狐");

		Hashtable ht1 = new Hashtable();
		Hashtable ht2 = new Hashtable();

		ht1.put("我的公文包", s1);
		ht1.put("我的电脑", s2);
		ht1.put("收藏夹", ht2);
		ht2.put("搜索", s3);
		ht2.put("门户", v4);

		JTree tree = new JTree(ht1);

		tree.putClientProperty("JTree.lineStyle", "Angled");
		
		JScrollPane jsp = new JScrollPane();
		jsp.setViewportView(tree);

		getContentPane().add(jsp);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new HashtableTreeDemo();
	}

}

// 用Object[]或者Vector建的树子节点没有"名字"
class VectorTreeDemo extends JFrame {

	public VectorTreeDemo() {
		super("我是用Vector建的树");
		setBounds(200, 200, 320, 500);

		Font font = new Font("Dialog", Font.PLAIN, 12);
		Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
		/** 定义Windows风格界面 **/
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			if (UIManager.get(key) instanceof Font) {
				UIManager.put(key, font);
			}
		}
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception el) {
			System.exit(0);
		}

		String[] s1 = { "公司文件", "个人信件", "私人文件" };

		Vector v1 = new Vector();

		v1.addElement(s1);
		v1.addElement("我的电脑");

		JTree tree = new JTree(v1);

		JScrollPane jsp = new JScrollPane();
		jsp.setViewportView(tree);

		getContentPane().add(jsp);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new VectorTreeDemo();
	}

}
